import CommonAction from '../Common/CommonAction'
import Common from '../Common/common';




export default class GameProp extends Laya.Scene {
    [x: string]: any;


    constructor() {
        super()

        Laya.stage.alignV = "middle";
        //水平居中对齐，另一种写法：Laya.stage.alignH = Stage.ALIGN_CENTER;
        Laya.stage.alignH = "center";

        Laya.Event.TRIGGER_ENTER
    }

    /**
     * 监听打开场景
     */
    onOpened() {
        if(Common.getCacheItem("gemstone") != null){
            this.money.text = "X "+Common.getCacheItem("gemstone")
        }else{
            this.money.text = "X 0"
        }
        if(Common.getCacheItem("character") != null){
            this.box.getChildByName("selection" + Common.getCacheItem("character")).visible = true
        }else{
            this.box.getChildByName("selection1").visible = true
        }
        var filter: Laya.GlowFilter = new Laya.GlowFilter("#936bed", 6, 0, 0);
        this.title.filters = [filter,filter]
        this.selectProp()
        this.buyProp()
        this.btnEvent()
    }

    /**
     * 监听鼠标事件
     */
    btnEvent() {
        Laya.stage.on("getProp",this,this.getProp)
        this.start.on(Laya.Event.CLICK, this, () => {
            Laya.Scene.open("gameView.scene");
            Laya.stage.event("playSound",["sound/tap.mp3"])
        });
        this.return.on(Laya.Event.CLICK, this, () => {
            this.close()
            Laya.stage.event("playSound",["sound/tap.mp3"])
        })
    }

    /**
     * 选择道具
     */
    selectProp() {
        for (let index = 1; index < 7; index++) {
            let buyed = Common.getCacheItem("buyed" + index);
            if (buyed == null) {
                this.getChildByName("select" + 1).on(Laya.Event.CLICK, this, this.selectionProp, [1])
            } else if (buyed == "1") {
                this.getChildByName("select" + index).on(Laya.Event.CLICK, this, this.selectionProp, [index])
            }
        }
    }

    selectionProp(index) {
        Laya.stage.event("playSound",["sound/tap.mp3"])
        this.box.getChildByName("selection" + index).visible = true
        Common.setCacheItem("character", index)
        for (let i = 1; i < 7; i++) {
            if (i != index) {
                this.box.getChildByName("selection" + i).visible = false
            }
        }
    }

    /**
     * 购买道具
     */
    buyProp() {
        for (let index = 1; index < 7; index++) {
            let buyed = Common.getCacheItem("buyed" + index);
            if (buyed == null) {
                if (index == 1) {
                    this.getChildByName("flaunt" + 1).on(Laya.Event.CLICK, this, this.flauntProp,[1])
                } else if (index > 1) {
                    this.getChildByName("buy" + index).visible = true
                    this.getChildByName("flaunt" + index).visible = false
                    this.getChildByName("buy" + index).on(Laya.Event.CLICK, this, this.buyingProp, [index])
                }
            } else if (buyed == "1") {
                this.getChildByName("buy" + index).visible = false
                this.getChildByName("flaunt" + index).visible = true
                this.getChildByName("flaunt" + index).on(Laya.Event.CLICK, this, this.flauntProp,[index])
            }
        }
    }

    /**
     * 购买道具动作
     */
    buyingProp(index) {
        Laya.stage.event("playSound",["sound/tap.mp3"])
        let money
        if(Common.getCacheItem("gemstone") != null){
            money = Number(Common.getCacheItem("gemstone"))
        }else{
            money = 0
        }
        let buy = this.getChildByName("buy" + index)
        let flaunt = this.getChildByName("flaunt" + index)
        if (index == 2) {
            console.log("看视频")
            Common.lastRewardedVideoEventName = "getProp"
            Laya.stage.event("showRewardedVideoAd")
        } else if (index == 3) {
            if (money >= 500) {
                Common.setCacheItem("gemstone", money - 500)
                Common.setCacheItem("buyed" + index, "1")
                buy.visible = false
                flaunt.visible = true
                this.getChildByName("select" + index).on(Laya.Event.CLICK, this, this.selectionProp, [index])
                this.getChildByName("flaunt" + index).on(Laya.Event.CLICK, this, this.flauntProp, [index])
            } else {
                console.log('余额不足')
                Laya.stage.event("ShowMsg","宝石不足，无法兑换")
            }
        } else if (index == 4) {
            if (money >= 600) {
                Common.setCacheItem("gemstone", money - 600)
                Common.setCacheItem("buyed" + index, "1")
                buy.visible = false
                flaunt.visible = true
                this.getChildByName("select" + index).on(Laya.Event.CLICK, this, this.selectionProp, [index])
                this.getChildByName("flaunt" + index).on(Laya.Event.CLICK, this, this.flauntProp, [index])
            } else {
                Laya.stage.event("ShowMsg","宝石不足，无法兑换")
            }
        } else if (index == 5) {
            if (money >= 800) {
                Common.setCacheItem("gemstone", money - 800)
                Common.setCacheItem("buyed" + index, "1")
                buy.visible = false
                flaunt.visible = true
                this.getChildByName("select" + index).on(Laya.Event.CLICK, this, this.selectionProp, [index])
                this.getChildByName("flaunt" + index).on(Laya.Event.CLICK, this, this.flauntProp, [index])
            } else {
                Laya.stage.event("ShowMsg","宝石不足，无法兑换")
            }
        } else if (index == 6) {
            if (money >= 1000) {
                Common.setCacheItem("gemstone", money - 1000)
                Common.setCacheItem("buyed" + index, "1")
                buy.visible = false
                flaunt.visible = true
                this.getChildByName("select" + index).on(Laya.Event.CLICK, this, this.selectionProp, [index])
                this.getChildByName("flaunt" + index).on(Laya.Event.CLICK, this, this.flauntProp, [index])
            } else {
                Laya.stage.event("ShowMsg","宝石不足，无法兑换")
            }
        }
        if(index != 2){
            this.money.text = "X "+Common.getCacheItem("gemstone")
        }
    }


    /**
     * 看长视频获取道具
     */
    getProp() {
        let buy = this.getChildByName("buy2")
        let flaunt = this.getChildByName("flaunt2")
        Common.setCacheItem("buyed2" , "1")
        buy.visible = false
        flaunt.visible = true
        this.getChildByName("select2").on(Laya.Event.CLICK, this, this.selectionProp,[2])
        this.getChildByName("flaunt2").on(Laya.Event.CLICK, this, this.flauntProp, [2])
    }

    /**
     * 炫耀分享
     */
    flauntProp(index) {
        Laya.stage.event("playSound",["sound/tap.mp3"])
        console.log("flauntProp",index)
        Laya.stage.event("wxShare",[index,''])
    }

}